/**
 * created by 江心才子, 2019/8/16
 * Copyright (c) 2019, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl.home.leaderboard

import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.ObservableBody
import com.wyl.base.HOME_VIDEO_CONTRIBUTION
import com.wyl.base.bean.UserData
import com.wyl.libbase.base.PageViewModel

/**
 * 项目名称：ShortVideo
 * 创建人：江心才子
 * 创建时间：2019-08-16 10:13
 * 内容描述：
 * 修改说明：
 */
class LeaderboardViewModel : PageViewModel() {
    var id = ""

    val dataSource = ObservableArrayList<Any>()

    val one = MutableLiveData<UserData>()
    val two = MutableLiveData<UserData>()
    val three = MutableLiveData<UserData>()

    override fun refresh() {
        OkGo.post<BaseResponse<List<UserData>>>(HOME_VIDEO_CONTRIBUTION)
            .params("appFolkOperaId", id)
            .converter(object : JsonConvert<BaseResponse<List<UserData>>>() {})
            .adapt(ObservableBody<BaseResponse<List<UserData>>>())
            .subscribe(
                { data ->
                    if (data.code == 1) {
                        empty.set(View.VISIBLE)
                    } else {
                        data.data.apply {
                            try {
                                one.value = this[0]
                                dataSource.add(this@LeaderboardViewModel)
                                two.value = this[1]
                                three.value = this[2]
                                dataSource.addAll(subList(3, size))
                            } catch (e: IndexOutOfBoundsException) {
                                e.printStackTrace()
                            }
                        }
                    }
                }, {
                    onError(it)
                    it.printStackTrace()
                }
            ).toDisposables(disposables)

    }

    override fun loadMore() {

    }

    init {
        enableRefresh.set(false)
        enableLoadMore.set(false)
//        one.value = USERDATA()
//        two.value = USERDATA()
//        three.value = USERDATA()
    }
}